<extend name="./Public/base.html"/>
<block name="body">


    <div style="width:90%;margin: 0 auto;">
        <h3>创建数据模型</h3>

        <form action="{:U('action')}" method="post" class="form-inline">
            <div class="form-group" style="padding-bottom: 15px;">
                <code>请选择项目的模块:</code>
                <select name="models" class="form-control select select-default">
                    <foreach name="models" item="v">
                        <option value="{$v.name}">{$v.name}</option>
                    </foreach>
                </select>
            </div>
            <div class=" pull-right">
                <a href="javascript:" class="fui-plus" id="add_field">&nbsp;添加字段</a>
            </div>
            <br/>
            <code>数据库(模块)信息:</code>

            <div class="well">
                <div class="row">
                    <div class="form-group">
                        <label for="table_name">数据表(模块英文)名称 : </label>
                        <input type="text" name="table_name" required placeholder="请输入表名称" class="form-control"/>
                    </div>
                    <div class="form-group">
                        <label for="table_comment">数据表(模块中文名称)注释 : </label>
                        <input type="text" name="table_comment" required placeholder="请输入表注释" class="form-control"/>
                    </div>
                    <div class="database" style="display: inline-block">
                        表引擎 :
                        <select name="engine" class="form-control select select-default">
                            <option value="INNODB">INNODB</option>
                            <option value="MYISAM">MYISAM</option>
                        </select>
                    </div>
                </div>
            </div>
            <code>创建类型:</code>

            <div class="well">
                <label>数据库:</label>
                <input type="checkbox" checked data-toggle="switch" name="databases" id="switch-database" value="1"/>
                <label>控制器:</label>
                <input type="checkbox" checked data-toggle="switch" name="controller" id="switch-controller" value="1"/>
                <label>模型:</label>
                <input type="checkbox" checked data-toggle="switch" name="model" id="switch-model" value="1"/>
                <label>视图:</label>
                <input type="checkbox" checked data-toggle="switch" name="view" id="switch-view" value="1"/>
            </div>
            <code>字段信息:</code>
            <table class="table well">
                <tr>
                    <th>字段(表单)名称</th>
                    <th class="database">字段类型</th>
                    <th class="database">长度</th>
                    <th>表单类型</th>
                    <th>默认值</th>
                    <th class="database">索引</th>
                    <th class="database">A_I</th>
                    <th>注释(表单名称)</th>
                    <th>&nbsp;</th>
                </tr>
                <include file="Index:rows"/>
            </table>
            <button class="btn btn-primary btn-wide" onclick="return beforeSubmit()">Save</button>
        </form>
    </div>

</block>

<block name="js">
    <script type="text/javascript">
        //下拉框
        $("select").select2();
        //开关
        $('#switch-database').bootstrapSwitch().parent('div').click(function () {
            showDatabaseInfo();
        });
        $('#switch-controller').bootstrapSwitch();
        $('#switch-model').bootstrapSwitch();
        $('#switch-view').bootstrapSwitch();
        //点击添加增加一行
        $('#add_field').click(function () {
            $.ajax({
                url: "{:U('addrow')}",
                success: function (r) {
                    $('table').append(r);
                    showDatabaseInfo();
                }
            });
        });

        //判断是否创建数据库的标示,数据库字段显示隐藏处控制
        var databases = 1 ;

        //删除一行
        function romove_row(obj) {
            $(obj).parents('.template').remove();
        }
        //提交前提示信息
        function beforeSubmit() {
            //删除隐藏的字段
            $("td:hidden").remove();
            //判断是否又主键
            if(databases==1){
                var flag = false ;
                $('select[name="indexs[]"]').each(function(){
                    if($(this).val()=='primary key'){
                        flag = true ;
                    }
                });
                if(!flag){
                    alert('您没有创建主键信息【索引为：primary】');
                    return false;
                }
            }
            if (!confirm('请检查您要创建的类型，选择创建将会覆盖，默认值和字段类型必须符合。')) {
                return false;
            }
        }

        //点击数据库按钮或者加载完一行数据，判断是否显示数据库字段信息
        function showDatabaseInfo() {
            if ($('#switch-database')[0].checked) {
                databases = 1 ;
                $('.database').show();
            } else {
                databases = 0 ;
                $('.database').hide();
            }
        }
    </script>
</block>

